#include <QComboBox>
#include <QCheckBox>
#include <QPushButton>
#include <QBoxLayout>
#include <QHBoxLayout>
#include <QMetaObject>
#include <QLineEdit>
#include <QSizePolicy>
#include <QKeyEvent>
#include <QDebug>

#include "QyFindDialog.h"

QyFindDialog::QyFindDialog( QWidget * parent ) : QDialog(parent) {

	QWidget * lineBox = new QWidget(this);
	QWidget * optionBox = new QWidget(this);
	QHBoxLayout * lineBoxLayout = new QHBoxLayout(lineBox);
	QHBoxLayout * optionBoxLayout = new QHBoxLayout(optionBox);

	layoutButton = new QPushButton(lineBox);
	findTextEdit = new QComboBox(lineBox);
	boxLayout = new QBoxLayout( QBoxLayout::TopToBottom, this );
	regExpBox = new QCheckBox( tr("Reg Exp"), optionBox );
	caseSensitiveBox = new QCheckBox( tr("Case Sensitive"), optionBox );
	wordMatchesBox = new QCheckBox( tr("Word Matches"), optionBox );

	boxLayout->addWidget(lineBox);
	boxLayout->addWidget(optionBox);
	lineBoxLayout->addWidget(layoutButton);
	lineBoxLayout->addWidget(findTextEdit);
	lineBoxLayout->setMargin(0);
	optionBoxLayout->addWidget(regExpBox);
	optionBoxLayout->addWidget(caseSensitiveBox);
	optionBoxLayout->addWidget(wordMatchesBox);
	optionBoxLayout->setMargin(0);

	findTextEdit->setObjectName("findTextEdit");
	// findTextEdit->setDuplicatesEnabled(true);
	findTextEdit->setEditable(true);
	layoutButton->setObjectName("layoutButton");
	layoutButton->setText("lo");
	layoutButton->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
	layoutButton->hide();

	setWindowTitle( tr("Find dialog") );
	QMetaObject::connectSlotsByName(this);

}

int QyFindDialog::direction() const {

	return (int) boxLayout->direction();
}

QString QyFindDialog::text() const {

	return findTextEdit->currentText();
}

void QyFindDialog::setDirection( int direction ) {

	boxLayout->setDirection( (QBoxLayout::Direction) direction );

}

void QyFindDialog::setText( const QString & text ) {

	findTextEdit->setEditText(text);

}

void QyFindDialog::findTextEditReturnPressed() {

	QString findText( findTextEdit->currentText() );

	if ( findText.isEmpty() )
		return;

	// qDebug( "Find: %s", qPrintable(findText) );

	// qDebug() << "state"
		// << ( regExpBox->checkState() == Qt::Checked )
		// << ( caseSensitiveBox->checkState() == Qt::Checked )
		// << ( wordMatchesBox->checkState() == Qt::Checked )
	// ;

	close();

	emit initFind( findText,
		regExpBox->checkState() == Qt::Checked,
		caseSensitiveBox->checkState() == Qt::Checked,
		wordMatchesBox->checkState() == Qt::Checked
	);

}

void QyFindDialog::on_layoutButton_clicked() {

	boxLayout->setDirection( ( boxLayout->direction() != QBoxLayout::TopToBottom ?
		QBoxLayout::TopToBottom : QBoxLayout::LeftToRight ) );

}

void QyFindDialog::keyPressEvent( QKeyEvent * event ) {

	int key = event->key(),
		modifiers = event->modifiers();

	if ( key == Qt::Key_Return && modifiers == Qt::NoModifier )
		findTextEditReturnPressed();
	else if ( key == Qt::Key_Escape && modifiers == Qt::NoModifier )
		close();

}
